import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:grab_the_order/app/modules/permission_check/permission_check_controller.dart';
import 'package:grab_the_order/core/values/app_styles.dart';

class PermissionCheckView extends GetView<PermissionCheckController> {
  const PermissionCheckView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("权限自检中心"),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          children: [
            // 【新增】通知权限检查项
            _buildPermissionItem(
              title: "通知权限",
              description: "用于在状态栏显示服务运行状态",
              status: controller.isNotificationGranted,
              onTap: controller.requestNotificationPermission,
            ),
            const SizedBox(height: 16),
            _buildPermissionItem(
              title: "电池优化白名单",
              description: "确保服务在后台不被系统“杀死”",
              status: controller.isBatteryOptimizationIgnored,
              onTap: controller.requestBatteryOptimization,
            ),
            const SizedBox(height: 16),
            _buildPermissionItem(
              title: "无障碍服务",
              description: "点击这里跳转到系统页面进行开启或确认",
              status: controller.isAccessibilityConfirmed,
              onTap: controller.gotoAccessibilitySettings,
            ),
            const Spacer(),
            ElevatedButton.icon(
              style: ElevatedButton.styleFrom(
                backgroundColor: kPrimaryColor,
                foregroundColor: Colors.white,
                padding:
                    const EdgeInsets.symmetric(horizontal: 30, vertical: 12),
                textStyle:
                    const TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
              ),
              icon: const Icon(Icons.refresh),
              label: const Text("重新检测"),
              onPressed: controller.checkAllPermissions,
            )
          ],
        ),
      ),
    );
  }

  Widget _buildPermissionItem({
    required String title,
    required String description,
    required RxBool status,
    required VoidCallback onTap,
  }) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        padding: const EdgeInsets.all(16),
        decoration: modernDecoration(),
        child: Row(
          children: [
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    title,
                    style: TextStyle(
                      fontSize: 17,
                      fontWeight: FontWeight.bold,
                      color: Colors.grey[800],
                    ),
                  ),
                  const SizedBox(height: 5),
                  Text(
                    description,
                    style: const TextStyle(fontSize: 14, color: Colors.grey),
                  ),
                ],
              ),
            ),
            const SizedBox(width: 10),
            Obx(
              () => status.value
                  ? const Icon(Icons.check_circle,
                      color: Colors.green, size: 30)
                  : const Icon(Icons.cancel, color: Colors.red, size: 30),
            ),
          ],
        ),
      ),
    );
  }
}
